import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/home")
public class Home extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //客户端发送请求后，检查 sessionId 是否正确
        //如果没有登录， session 就默认返回 null
        req.setCharacterEncoding("utf8");
        HttpSession session = req.getSession(false);
        //读取主页信息
        String username = (String) session.getAttribute("username");
        long loginTime = (long) session.getAttribute("loginTime");
        String welcome = "欢迎来到主页，" + username + "! 登录时间：" + loginTime;
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write(welcome);
    }
}
